home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / Tool.java < prev    next >
Encoding:
Java Source  |  2000-07-25  |  3.2 KB  |  127 lines

  1. /*
  2.  * Tool.java - Tool selector
  3.  * Copyright (C) 2000 Romain Guy
  4.  * guy.romain@bigfoot.com
  5.  * www.jext.org
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  */
  21.  
  22. import waba.fx.*;
  23.  
  24. /**
  25.  * <code>Tool</code> is a tool selector. By tapping it or pressing
  26.  * the appropriate hard key, the user can switch between available
  27.  * tools..
  28.  * @author Romain Guy <guy.romain@bigfoot.com>
  29.  * @version 1.0
  30.  */
  31.  
  32. public class Tool extends Bounded
  33. {
  34.   // tool to fill in the cells
  35.   public static final byte FILLING_TOOL = 0;
  36.   // tool to check the cells
  37.   public static final byte HELPER_TOOL = 1;
  38.   // tool to erase a cell
  39.   public static final byte ERASER_TOOL = 2;
  40.   // amount of tools
  41.   public static final byte TOOLS_COUNT = 3;
  42.  
  43.   // tools pictures
  44.   private Image[] pictures = new Image[TOOLS_COUNT];
  45.   // selected tool
  46.   private byte tool = FILLING_TOOL;
  47.  
  48.   public Tool()
  49.   {
  50.     for (int i = 0; i < pictures.length; i++)
  51.       pictures[i] = new Image("datas/tool" + i + ".bmp");
  52.   }
  53.  
  54.   public void free()
  55.   {
  56.     for (int i = 0; i < pictures.length; i++)
  57.       pictures[i].free();
  58.   }
  59.  
  60. //////////////////////////////////
  61. // NOT REQUESTED YET            //
  62. //////////////////////////////////
  63. //  public void setTool(byte tool)
  64. //  {
  65. //    this.tool = tool;
  66. //  }
  67. //////////////////////////////////
  68.  
  69.   public byte getTool()
  70.   {
  71.     return tool;
  72.   }
  73.  
  74.   /**
  75.    * Handles a pen down event.
  76.    * @param x The x coordinate of the event
  77.    * @param y The y coordinate of the event
  78.    * @return <code>true</code> if the event can be handled
  79.    */
  80.  
  81.   public boolean handlePenEvent(int x, int y)
  82.   {
  83.     return (x > this.x && x < (this.x + this.width) &&
  84.             y > this.y && y < (this.y + this.height));
  85.   }
  86.  
  87.   /**
  88.    * Switches current tool going through list forward.
  89.    */
  90.  
  91.   public void switchToolForward(eCross e, Graphics g)
  92.   {
  93.     if (++tool == TOOLS_COUNT)
  94.      tool = FILLING_TOOL;
  95.     paintTool(e, g);
  96.   }
  97.  
  98.   /**
  99.    * Switches current tool going through list backward.
  100.    */
  101.  
  102.   public void switchToolBackward(eCross e, Graphics g)
  103.   {
  104.     if (--tool == -1)
  105.      tool = ERASER_TOOL;
  106.     paintTool(e, g);
  107.   }
  108.  
  109.   private void paintTool(eCross e, Graphics g)
  110.   {
  111.     g.drawImage(pictures[tool], x + 3, y + 2);
  112.     e.drawTool();
  113.   }
  114.  
  115.   public void paint(Graphics g)
  116.   {
  117.     g.setColor(255, 255, 255);
  118.     g.fillRect(x, y, width, height);
  119.     g.setColor(0, 0, 0);
  120.     g.drawRect(x, y, width, height);
  121.  
  122.     g.drawImage(pictures[tool], x + 3, y + 2);
  123.   }
  124. }
  125.  
  126. // End of Tool.java
  127.